package com.sumainfor.pc.epc.service.impl.FeignMkt;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.ArticleMapper;
import com.sumainfor.pc.epc.dao.MktFeign.HousingRepairMapper;
import com.sumainfor.pc.epc.dao.UtilsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述:
 * 管家（市场部）发起维修工单
 * @Auther: zhlu
 * @Date: 2019/3/8 20:00
 * @Description:
 */
@Service
public class HousingRepairService {
    private ReentrantLock lock = new ReentrantLock(false);

    @Autowired
    HousingRepairMapper HousingRepairMapper;

    @Autowired
    ArticleMapper ArticleMapper;

    @Autowired
    UtilsMapper utilsMapper;

    String UUID=null;

    /**
     * 功能描述: 
     * 管家新增微信工单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/8 20:02
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean AddRepair(Map<String,Object> params){
        boolean result=false;
        lock.lock();
        try {
            try {
                List<Map<String, Object>> repair = (List<Map<String, Object>>) params.get("repair");
                for(Map<String, Object> map:repair){
                    Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                        put("uuid", "RepId");
                        put("database", Constants.DATA_SOURCE_TCEPC);
                        put("surface", "tcepc_repair");
                    }});
                    if(uuid!=null){
                        int number=Integer.valueOf(uuid.get("uuid").toString());
                        UUID= ComUtils.getLocalTrmSeqNum("wxdg",number);
                    }else{
                        UUID=ComUtils.getLocalTrmSeqNum("wxdg",0);
                    }

                    //所有新增处理
                    Map<String,Object>commonInsertParms=new HashMap<String,Object>();
                    commonInsertParms.put("by", ContextUtils.getUserId());//操作人
                    commonInsertParms.put("RepairsId", ContextUtils.getUserId());//报修人编号
                    commonInsertParms.put("RepId",UUID);//维修工单编号
                    commonInsertParms.put("term", "4");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    commonInsertParms.put("fnc",
                            this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(commonInsertParms);
                    map.putAll(commonInsertParms);
                    map.put("RepType",2);//报修人类型 （1.普通用户，2.管家）
                    if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"HouCon"})&&FieldCheckUtil.isMapFieldNotNull(map,new String[]{"ArtTypeId"})&&FieldCheckUtil.isMapFieldNotNull(map,new String[]{"ArtId"})&&FieldCheckUtil.isMapFieldNotNull(map,new String[]{"TroId"})){
                        //新增维修工单
                        HousingRepairMapper.AddRepair(map);
                    }
                    result=true;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }

    /**
     * 功能描述: 
     * 根据管家巡房主编号获取管家维修集合
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/9 15:02
     */
    public List<Map<String,Object>>getHousingRepairList(Map<String,Object>params){
        List<Map<String,Object>>getHousingRepairList=HousingRepairMapper.getHousingRepairList(params);
        if(getHousingRepairList.size()>0){
            for(Map<String,Object> map:getHousingRepairList){
                map.put("ArtTypeIdList",ArticleMapper.getArticleTypeList(new HashMap<String, Object>(){{
                    put("delfg",0);
                }}));
                map.put("ArtIdList",ArticleMapper.getArticleList(new HashMap<String, Object>(){{
                    put("delfg",0);
                }}));
                map.put("TroIdList",ArticleMapper.getArticleTroubleList(new HashMap<String, Object>(){{
                    put("delfg",0);
                }}));
            }
        }
        return getHousingRepairList;
    }

    /**
     * 功能描述: 
     * 市场部经理获取维修中房源 ----
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/16 20:14
     */
    public Integer getMatketHousing(){
        return HousingRepairMapper.getMatketHousing();
    }

}
